<template>
  <div class="wt-swiper">
    <div class="swiper-container">
      <div ref="wrapper" class="swiper-wrapper">
        <div class="swiper-slide" v-for="(slide,index) in slideList" :key="index" :style="{transform: `translateX(${translateX}px)`,'transition-duration': `${duration}s`,'margin-right':`${gap}px`}">
          <slot name="slide" :item="slide" />
        </div>
      </div>
    </div>
    <div v-show="translateX < max" class="button prev-button" @click="slide(1)">
      <svg-icon iconClass="left-arrow" />
    </div>
    <div v-show="translateX > min" class="button next-button" @click="slide(-1)">
      <svg-icon iconClass="right-arrow" />
    </div>
  </div>
</template>

<script type="text/javascript">
import { debounce } from '@/utils'
export default {
  name: 'WtSwiper',
  components: {

  },
  props: {
    slideList: {
      type: Array,
      required: true
    },
    gap: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      slideLen: 0, // 左右切换时移动的距离
      scrollWidth: 0, // 轮播容器大小
      offsetWidth: 0, // 外层容器大小
      translateX: 0, // 偏移量
      duration: 0 // 移动时间
    }
  },
  created () {
    this.resizeHandler = debounce(this.resizeHandler)
    window.addEventListener('resize', this.resizeHandler)
  },
  methods: {
    resizeHandler () {
      this.$nextTick(this.init)
    },
    init () {
      this.duration = this.translateX = 0
      this.scrollWidth = this.$refs.wrapper.scrollWidth
      const width = this.scrollWidth / this.slideList.length
      this.offsetWidth = this.$refs.wrapper.offsetWidth
      this.slideLen = parseInt(this.offsetWidth / width) * width
    },
    slide (step) {
      const { max, min } = this
      let move = this.translateX
      move += step * this.slideLen
      if (move < min)move = min
      if (move > max)move = max
      this.duration = Math.round(Math.abs((move - this.translateX)) / 500) * 0.5
      this.$nextTick(() => {
        this.translateX = move
      })
    }
  },
  computed: {
    max () {
      return 0
    },
    min () {
      return this.offsetWidth - this.scrollWidth
    }
  },
  watch: {
    slideList: {
      handler (newVal) {
        if (Array.isArray(newVal) && newVal.length) {
          this.$nextTick(this.init)
        }
      },
      immediate: true
    }
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.resizeHandler)
  }
}
</script>

<style lang="scss" scoped>
.wt-swiper {
  position: relative;
  padding: 0 20px;
  margin: 0 -20px;
  .swiper-container {
    overflow: hidden;
    .swiper-wrapper {
      position: relative;
      /* width: 100%;
      height: 100%; */
      z-index: 1;
      display: flex;
      transition-property: transform;
    }
    .swiper-slide {
      display: inline-block;
      flex: 224px 0 0;
    }
  }
  .button {
    display: flex;
    position: absolute;
    width: 40px;
    height: 40px;
    top: 50%;
    z-index: 999;
    cursor: pointer;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
    background-color: #efefef;
    transform: translateY(-50%);
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    &:hover {
      background-color: #ddd;
    }
    &.swiper-button-disabled {
      display: none;
    }
    .svg-icon {
      color: #333;
      font-size: 16px;
    }
  }
  .prev-button {
    left: 0;
  }
  .next-button {
    right: 0;
  }
}
</style>
